package com.book.controller.admin;

import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.book.common.BaseController;
import com.book.entity.BookChapter;
import com.book.entity.BookInfo;
import com.book.entity.BookType;
import com.github.drinkjava2.jsqlbox.ActiveRecord;
import com.github.drinkjava2.jsqlbox.DB;
import com.github.drinkjava2.jsqlbox.entitynet.EntityNet;
import com.github.drinkjava2.jsqlbox.handler.EntityNetHandler;


/**
*
*     小说信息控制类
*
*/
public class ChapterController  extends BaseController<BookChapter> {

	
	public ChapterController() {
		super();
	}
	
	@Override
		public String edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		
		
		String id_str = req.getParameter("id");
		
		int id = Integer.parseInt(id_str);
		
		BookChapter eo = getRecord().loadById(id);
		BookInfo bieo = new BookInfo().loadById(eo.getBookId());
		
		req.setAttribute("eo", eo);
		
		req.setAttribute("bieo", bieo);
		
		return String.format("/%s/edit",getModule() );
	
	
	
	
	
	}
	
	
	
	public String add(HttpServletRequest req, HttpServletResponse resp ) throws Exception {

		System.out.println("进入 ChapterController 的 add 方法。。。。");
	
		String bookId = req.getParameter("id");
		BookInfo eo = new BookInfo().loadById(Integer.parseInt(bookId));
		
		req.setAttribute("bieo", eo);
		
		return String.format("/%s/edit",getModule() );
	
	}
	
	public String query(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		System.out.println("进入 ChapterController.java   的方法");
		String bookId = req.getParameter("bookId");
		
		
		
		if(bookId == null || "".equals(bookId.trim())) {

			  EntityNet net = getRecord().autoNet(BookChapter.class,BookInfo.class);
			  List<BookChapter> chapterlist =net.pickEntityList(BookChapter.class);
			  req.setAttribute("datalist", chapterlist);
			  return String.format("/%s/list", getModule());
		 }else {
			  List<BookChapter> chapterlist = DB.iQueryForEntityList(BookChapter.class,
					   "select bc.*,bi.book_name,bi.book_author from book_chapter bc ", 
			           "INNER JOIN book_info bi on bc.book_id=bi.book_id ",
					   "where",
			           DB.noNull(" bc.book_id like ?","%",bookId,"%"));
			
			  req.setAttribute("datalist", chapterlist);
			  return String.format("/%s/list", getModule());
		 }
		
		
		
		
		
	}
	
	
	
	public String index(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		System.out.println("进入 ChapterController.java   的方法");
		
		
		  EntityNet net = getRecord().autoNet(BookChapter.class,BookInfo.class);
		  List<BookChapter> chapterlist =net.pickEntityList(BookChapter.class);
	
		
		  req.setAttribute("datalist", chapterlist);
		
		
		return String.format("/%s/list", getModule());
	}
	
	

	public ActiveRecord<BookChapter>  getRecord() {
		return new BookChapter();
	}
	
	public String  getPKFielName() {
		return "chapterId";
	}
	
	@Override
	public String getModule() {
		return "chapter";
	}
}
